<%-- 
    Document   : GioHang
    Created on : Oct 8, 2012, 11:35:59 PM
    Author     : KhanhNhac
--%>

<%@page import="dao.SanPhamDAO"%>
<%@page import="dao.KhachHangDAO"%>
<%@page import="dao.DonHangDAO"%>
<%@page import="pojo.ChiTietDonHangId"%>
<%@page import="Dao.ulti.ChiTietDonDatHangDAO"%>
<%@page import="pojo.ChiTietDonHang"%>
<%@page import="java.util.Date"%>
<%@page import="Dao.ulti.KhachHangDAO"%>
<%@page import="Dao.ulti.DonHangDAO"%>
<%@page import="pojo.DonHang"%>
<%@page import="Dao.ulti.SanPhamDAO"%>
<%@page import="pojo.SanPham"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/Shoes.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body background="images/Blue_Background.png">
      <%
            ArrayList<SanPham> gioHang;
            session = request.getSession();
            if (session.getAttribute("GioHang") == null) {
                gioHang = new ArrayList<SanPham>();
                session.setAttribute("GioHang", gioHang);
            } else {
                gioHang = (ArrayList<SanPham>) session.getAttribute("GioHang");
            }
            String action = request.getParameter("action");
            if (action != null) {
                if (action.equals("DatMua")) {
                    String maSanPham = request.getParameter("maSanPham");
                    boolean kq = false;
                    for (int i = 0; i < gioHang.size(); i++) {
                        SanPham s = gioHang.get(i);
                        if (s.getMaSanPham().equals(maSanPham)) {
                            s.setSoLuong(s.getSoLuong() + 1);
                            kq = true;
                            break;
                        }
                    }
                    if (kq == false) {
                        SanPham s = SanPhamDAO.layThongTinSanPham(maSanPham);
                        s.setSoLuong(1);
                        gioHang.add(s);
                    }
                    
                } else if (action.equals("Xoa")) {
                    String maSanPham = request.getParameter("maSanPham");
                    for (int i = 0; i < gioHang.size(); i++) {
                        if (gioHang.get(i).getMaSanPham().equals(maSanPham)) {
                            gioHang.remove(i);
                            break;
                        }
                    }
                }
            }
            String capNhat = request.getParameter("capNhat.x");
            if (capNhat != null) {
                String[] s = request.getParameterValues("soLuong");
                for (int i = 0; i < gioHang.size(); i++) {
                    int soluong = Integer.parseInt(s[i]);
                    gioHang.get(i).setSoLuong(soluong);
                }
            }
            String thanhToan = request.getParameter("thanhToan.x");
            if (thanhToan != null) {
                int Kh =2;
                DonHang h = new DonHang();
                int maDonHang = DonHangDAO.sinhMaDonHang(Kh);
                h.setMaDonHang(maDonHang);
                h.setKhachHang(KhachHangDAO.layThongTinKhachHang(Kh));
                h.setNgayMua(new Date());
                DonHangDAO.themDonHang(h);
                for (int i = 0; i < gioHang.size(); i++) {
                    SanPham s = gioHang.get(i);
                    ChiTietDonHang ct = new ChiTietDonHang();
                    String ctdh=String.valueOf(i)+String.valueOf(maDonHang);
                    int ctdh2= Integer.parseInt(ctdh);
                    ct.setId(new ChiTietDonHangId(ctdh2,maDonHang));
                    ct.setDonHang(h);
                    ct.setSanPham(s);
                    ct.setGia(s.getGiaBan());
                    ct.setSoLuong(s.getSoLuong());
                    
                    ChiTietDonDatHangDAO.themChiTietHoaDon(ct);
                }
                session.removeAttribute("GioHang");
                String url = "LuocSuMuaHang.jsp?action=xem&maDonHang=" + maDonHang+"&Kh="+Kh;
                response.sendRedirect(url);
                return;
                               }
    %>
<table width="1034" height="455" align="center" border="1" bordercolor="#FFFFFF">
  <tr align="center">
    <td colspan="5" valign="middle"><img src="images/BannerShoes.gif" width="1024" height="247"/></td>
  </tr>
  <tr>
    <td width="200" align="center"><a href="TrangChu"><img src="images/Button-Trangchu.gif" width="179" height="48"/></a></td>
    <td width="200" align="center"><a href="TrangDangKy"><img src="images/Button-Nam.gif" width="179" height="48"/></a></td>
    <td width="200" align="center"><a href="TrangChu"><img src="images/Button-Nu.gif" width="179" height="48"/></a></td>
    <td width="200" align="center"><a href="TrangChu"><img src="images/Button-Treem.gif" width="179" height="48"/></a></td>
    <td width="200" align="center"><a href="TrangChu"><img src="images/Button-Gioithieu.gif" width="179" height="48"/></a></td>
  </tr>
  <tr align="center">
    <td colspan="5">
    <p align="right"><font color="#0066FF"><strong>Tìm kiếm theo:</strong></font>
      <select name="kichCo2" size="1" style="width:15%">
        <option value="Tất cả" selected="selected">--Chọn thương hiệu--</option>
        <option>Guicci</option>
        <option>Puma</option>
      </select>
<select name="kichCo" style="width:15%">
                        <option value="Tất cả" selected="selected">--Chọn loại--</option>
                        <option value="mã danh mục">Tên danh mục</option>
                      </select>
        <select name="kichCo" style="width:15%">
                        <option value="Tất cả" selected="selected">--Chọn kích cỡ--</option>
                        <option value="mã danh mục">Tên danh mục</option>
        </select>
         <select name="kichCo" style="width:15%">
                        <option value="Tất cả" selected="selected">--Chọn giá--</option>
                        <option value="mã danh mục">Tên danh mục</option>
        </select>
            <input type="submit" value="Tìm kiếm" name="timKiem">           
    </p></td>
  </tr>
  <tr>
    <td height="294" align="center">
    <table width="100%" height="190" cellpadding="5" cellspacing="0" background="images/background.jpg">
              <tr>
                <td colspan="2" height="30" valign="top" background="images/Bartop.gif"><p><strong><font color="#FFFFFF">THƯƠNG HIỆU</font></strong></p></td>
              </tr>
                <!--BatDauDanhMuc-->
              <tr>
                <td>               
                 <a href="DanhSanPhamSanPham.jsp?maThuongHieu=1">Bitis</a></td>
              </tr> 
            </table>
    <table width="100%" background="images/background.jpg" >
      <tr>
        <td colspan="2" height="30" align="center" valign="top" background="images/Bartop.gif"><p><strong><font color="#FFFFFF">LOẠI SẢN PHẨM</font></strong></p></td>
        </tr>
      <tr>
        <td width="51"><img src="images/kids_dep.gif"/></td>
        <td width="137"><a><font color="#0000FF" >Dép</font></a></td>
      </tr>
      <tr>
        <td width="51"><img src="images/batavai_Icon.png"/></td>
        <td width="137"><a><font color="#0000FF" >Giầy thể thao</font></a></td>
      </tr>
      <tr>
        <td width="51"><img src="images/men_daytay.gif"/></td>
        <td width="137"><a><font color="#0000FF">Giầy tây</font></a></td>
      </tr>
      <tr>
        <td width="51"><img src="images/nu_thoitrang.gif"/></td>
        <td width="137"><font color="#0000FF"><a>Giầy cao gót</a></font></td>
      </tr>
      <tr>
        <td width="51"><img src="images/men_sandal.gif"/></td>
        <td width="137"><a><font color="#0000FF">Sandals</font></a></td>
      </tr>
      <tr>
        <td width="51"><img src="images/kids_hai.gif"/></td>
        <td width="137"><font color="#0000FF">Hài</font></td>
      </tr>
    </table></td>
    <td colspan="4" valign="top"><!-- InstanceBeginEditable name="MainContent" -->
    <table width="100%" border="1" cellspacing="0" cellpadding="5">
            <tr bgcolor="#666666">
              <td background="images/Bartop.gif"><strong><font color="#FFFFFF">THÔNG TIN NGƯỜI ĐẶT HÀNG</font></strong></td>
            </tr>
            <tr>
            	<td>
                	...
                </td>
            </tr>
            </table>
            <br/>
        	<table width="100%" border="1" cellspacing="0" cellpadding="5">
            <tr bgcolor="#666666">
              <td background="images/Bartop.gif"><strong><font color="#FFFFFF">THÔNG TIN NGƯỜI NHẬN HÀNG</font></strong></td>
            </tr>
            <tr>
            	<td>
                	...
                </td>
            </tr>
            </table>
            <br/>
        	 <table width="100%" border="1" cellspacing="0" cellpadding="5">
            <tr bgcolor="#666666">
              <td background="images/Bartop.gif"><strong><font color="#FFFFFF">THÔNG TIN HÌNH THỨC THANH TOÁN</font></strong></td>
            </tr>
            <tr>
            	<td>
                	...
                </td>
            </tr>
            </table>
            <br/>
             <table width="100%" border="1" cellspacing="0" cellpadding="5">
            <tr bgcolor="#666666">
              <td background="images/Bartop.gif"><strong><font color="#FFFFFF">THÔNG TIN GIỎ HÀNG</font></strong></td>
            </tr>
            <tr>
            	<td>
                <form name="gioHang" action="GioHang.jsp" method="get">
                <table width="100%" border="1" cellspacing="0" cellpadding="5">
            	  <tr align="center" bgcolor="#CCCCCC">
            	    <td width="6%"><strong>STT</strong></td>
            	    <td width="10%"><strong>HÌNH</strong></td>
            	    <td ><strong>TÊN SẢN PHẨM</strong></td>
            	    <td width="15%"><strong>S. LƯỢNG</strong></td>
            	    <td width="15%"><strong>G. BÁN</strong></td>
            	    <td width="15%"><strong>T.  TIỀN</strong></td>
            	    <td width="10%"><strong>XÓA</strong></td>
          	      </tr>
                  <%
                             double tongTien =0;
                    for (int i = 0; i < gioHang.size(); i++) {
                        SanPham s = gioHang.get(i);
                        tongTien += s.getGiaBan() * s.getSoLuong();
                  %>
            	  <tr valign="top">
            	    <td align="center" bgcolor="#FFFFFF"><%=i+1%></td>
            	    <td width="9%" align="center" bgcolor="#FFFFFF"><a href="#"><img src="images/GiayTheThao.png" alt="#" width="57" height="85" border="0" /></a></td>
            	    <td bgcolor="#FFFFFF"><a href="#"><%=s.getTenSanPham()%></a></td>
            	    <td width="12%" bgcolor="#FFFFFF"><label for="soLuong" ></label>
            	      <input name="soLuong" type="text" id="soLuong" value="<%=s.getSoLuong()%>" size="17" /></td>
            	    <td align="right" bgcolor="#FFFFFF"><%=s.getGiaBan()%></td>
            	    <td align="right" bgcolor="#FFFFFF"><%=s.getGiaBan()*s.getSoLuong()%></td>
            	    <td align="center" bgcolor="#FFFFFF"><a href="GioHang.jsp?action=Xoa&maSanPham=<%=s.getMaSanPham()%>"><img src="images/Stop.png" alt="#" width="60" height="60" border="0" align="bottom" /></a></td>
          	    </tr>
                  <%}%>
            	  <tr valign="top">
            	    <td colspan="5" align="right">Cước vận chuyển</td>
            	    <td colspan="2" align="center">...</td>
          	    </tr>
            	  <tr valign="top">
            	    <td colspan="5" align="right">Thuế</td>
            	    <td colspan="2" align="center">...</td>
          	    </tr>
            	  <tr valign="top">
            	    <td colspan="5" align="right">Tổng tiền</td>
            	    <td colspan="2" align="center"><strong><font color="#FF0000"><%=tongTien%> VNĐ</font></strong></td>
          	    </tr>            	 
          	    </table>
                <br/>
                <table width="100%">
                	 <tr valign="top">
                        <td align="center"><a href="DanhSachSanPham.jsp"><img src="images/Button-tieptucmua.png" alt="#" width="167" height="41" border="0" /></a></td>
                        <td  align="center"><input type="image"src="images/Button-capnhat.png" width="167" height="41" name="capNhat" /></td>
                         <td  align="center"><input type="image"src="images/Button-thanhtoan.png" width="167" height="41" name="thanhToan" /></td>
                    </tr>
                </table>
                </form>
                </td>
            </tr>
            </table>
			<!-- InstanceEndEditable --></td>
  </tr>
  <tr align="center">
    <td colspan="5" background="images/Bartop.gif">&nbsp;</td>
  </tr>
  <tr align="center">
    <td colspan="5">
      <p align="right"><a><font color="#0000FF"><strong>Đăng nhập &nbsp;&nbsp;  </strong></font></a> <a><font color="#0000FF"><strong>Đăng ký&nbsp;</strong></font></a></p></td>
  </tr>
  <tr align="center">
    <td colspan="5"><strong>@ 2012 Le Khanh Nhac </strong> <a href="#" target="_blank">Terms &amp; Privacy</a></td>
  </tr>
</table>
</body>
<!-- InstanceEnd --></html>

